//
//  EditViewController.m
//  项目三
//
//  Created by rimi1 on 2016/12/11.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "EditViewController.h"
#import "LookImgViewController.h"

#import "EditView.h"
#import "Alert.h"

#import "AVRequest+Relation.h"

@interface EditViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) EditView *editView;
@property (nonatomic, strong) UIImage *img;

@end

@implementation EditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
}

#pragma mark - initialize

- (void)initializeDataSource {
    
}

- (void)initializeAppearance {
    self.navigationBar.title.text = @"分享菜谱";
    self.navigationBar.right.frame = CGRectMake(kWIDTH -  10 - 60, 27, 60, 30);
    self.navigationBar.right.layer.cornerRadius = 5;
    
    [self.navigationBar.right setTitle:@"发布" forState:UIControlStateNormal];
    [self.navigationBar.right setBackgroundColor:[UIColor orangeColor]];
    self.view.backgroundColor = kCOLOR_RGB(245, 245, 245, 1);
    [self.view addSubview:self.editView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - URLRequest 

- (void)urlEditRequestWithName:(NSString *)name food:(NSString *)food step:(NSString *)step imgData:(NSData *)imgData {
    [AVRequest avEditWithName:name food:food step:step img:imgData sueeccd:^(BOOL succeed, NSError *error) {
        if (error) {
            [Alert alertWithTitle:@"上传失败" message:nil];
            return;
        }
        
        if (succeed) {
            // 当上传成功
            if (self.block) {
                self.block();
            }
            [MyUser sharedUser].myDynamicCount = [NSString stringWithFormat:@"%ld", [[MyUser sharedUser].myDynamicCount integerValue] + 1];
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [Alert alertWithTitle:@"上传失败" message:nil];
        }
        
    }];
}

#pragma mark - respondsTo..

- (void)respondsToRight:(UIButton *)sender {
    NSString *name = self.editView.name.text;
    NSString *food = self.editView.food.text;
    NSString *step = self.editView.step.text;
    UIImage *img = self.editView.img.currentBackgroundImage;
    UIImage *imgNO = [UIImage imageNamed:@"加"];
    if (name.length != 0 && food.length != 0 && step.length != 0 && ![img isEqual:imgNO] && img) {
        NSData *data = UIImagePNGRepresentation(img);
        [self urlEditRequestWithName:name food:food step:step imgData:data];
    }else {
        [Alert alertWithTitle:@"请填写完整" message:nil];
    }
}

- (void)respondsToAddImg:(UIButton *)sender {
    if (sender.selected) {
        LookImgViewController *look = [[LookImgViewController alloc] init];
        look.img = self.img;
        
        look.block = ^() {
            sender.selected = NO;
        };
        [self.navigationController pushViewController:look animated:YES];
    }else {
        [Alert alertWithPhoto:^{
            [self photoAlbum];
        } camera:^{
            [self takePhotos];
        }];
    }
}

#pragma mark - Camera
// 拍照
- (void)takePhotos {
    // 判断是否有前后摄像头(是否支持拍照)
    if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
        [Alert alertWithTitle:@"不支持拍照功能" message:nil];
        return;
    }
    // 设置相机
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 推出相机
    [self presentViewController:imagePicker animated:YES completion:nil];
}
// 相册
- (void)photoAlbum {
    // 设置相册
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imagePicker.delegate = self;
    // 进入编辑状态
    imagePicker.allowsEditing = YES;
    // 跳到系统相册
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
// 选中图片自动调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    [self.editView.img setBackgroundImage:image forState:UIControlStateSelected];
    self.img  = image;
    self.editView.img.selected = YES;
    [self dismissViewControllerAnimated:YES completion:nil];
}
// 取消选择自动调用(右上角的Cancel)
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - get

- (EditView *)editView {
    
    if (!_editView) {
        
//        CGFloat height = 30 + 88 + 10 + 14 + 5 + WXQAdaptionHeight(200) + 20 + WXQAdaptionHeight(60);
        _editView = [[EditView alloc] initWithFrame:CGRectMake(0, 64, kWIDTH, kHEIGHT - 64)];
        [_editView.img addTarget:self action:@selector(respondsToAddImg:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _editView;
}

@end













